/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import net.comm.check.service.impl.JifenServiceImpl;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.oper.service.impl.OperLogServiceImpl;
import net.comm.oper.web.form.OperLogForm;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;


import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;
import net.comm.group.bean.BaseGForum;
import net.comm.group.db.DbGForumService;
import net.comm.group.db.DbGroupsService;
import net.comm.group.db.GForumServiceSqlMysql;
import net.comm.group.db.GroupsServiceSqlMysql;
import net.comm.group.web.form.GFollForumForm;
import net.comm.group.web.form.GForumForm;
import net.comm.group.web.form.GroupsForm;
import net.comm.page.PageCtr;

import net.comm.tags.JstlFun;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;
import net.comm.util.StringUtil;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.HtmlPage;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GForumServiceImpl {

	static Logger logger = Logger.getLogger(GForumServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GForumForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getQryGroupID();
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = page.getPos()-1;
			fields[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
	/**
	 * 查询我加入的话题
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listbyFollUserID(GForumForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			Object[] fields2 = new Object[3];
			
			fields2[0] = theForm.getQryGroupID();
			fields2[1] = theForm.getQryUserID();
			fields2[2] = theForm.getQryUserID();
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql4();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[5];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = theForm.getQryUserID();
			fields[2] = theForm.getQryUserID();
			fields[3] = page.getPos()-1;
			fields[4] = page.getPageSize();
			
			sql = servSql.getRecbyFolluserIDSql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}	
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List zllist(GForumForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getQryVillageID();
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryVillageID();
			fields[1] = page.getPos()-1;
			fields[2] = page.getPageSize();
			
			sql = servSql.getRecbyvillageIDPageSql();

			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}	
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List adlist(GForumForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1 ");
			
			int qryGroupID = theForm.getQryGroupID();
			if(qryGroupID > 0 && qryGroupID != GlbFinal.PUBLICID){
				
				sb.append(" and GroupID = ");
				sb.append(qryGroupID);
				sb.append(" ");
			}			
			int qryVillageID = theForm.getQryVillageID();
			if(qryVillageID > 0){
				
				sb.append(" and VillageID = ");
				sb.append(qryVillageID);
				sb.append(" ");
			}			
			
			int qryUserID = theForm.getQryUserID();
			if(qryUserID > 0){
				
				sb.append(" and userID = ");
				sb.append(qryUserID);
				sb.append(" ");
			}			
			String qryTitle = theForm.getQryTitle().replaceAll(GlbFinal.SQLINjection, " ");
			if(!qryTitle.equals("")){
				
				sb.append(" and Title like '%");
				sb.append(qryTitle);
				sb.append("%' ");
			}			
			int qryFirsort = theForm.getQryFirsort();
			if(qryFirsort > 0){
				
				sb.append(" and Firsort = ");
				sb.append(qryFirsort);
				sb.append(" ");
			}			
			String qryOrderby = theForm.getQryOrderby().replaceAll(GlbFinal.SQLINjection, " ");
			if(!qryOrderby.equals("")){
				
				sb.append(" Order by ");
				sb.append(qryOrderby);
				sb.append(" ");
				String qryDesc = theForm.getQryDesc().replaceAll(GlbFinal.SQLINjection, " ");
				if(!qryDesc.equals("")){					
					sb.append(qryDesc);
					sb.append(" ");
				}
			}else{
				
				sb.append(" Order by Ctime desc ");
			}

			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql = servSql.getCountSql3();

			logger.debug(sql+sb.toString());
			
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();
			
			if(theForm.getIsradnom().equals(GlbFinal.YES)){

				int pos = (int)(Math.random()*(page.getTotalNum()-20));
				page.setPos(pos);
			}			

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecSql();
			 logger.debug(sql+sb.toString());
			 
			 DbGForumService dbservice = new DbGForumService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbservice.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}		
		return list;
	}
	
	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List list(int  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = userID;			
			fields[1] = DatetimeUtil.getDateByadd(DatetimeUtil.getDatetime(),
					Calendar.DAY_OF_MONTH, -7,"yyyyMMddhhmmss");			

			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	
	/**
	 * 返回根据First查询的群组
	 */
	public List listGroupbyFirsort(GForumForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getQryVillageID();			

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getGroupByFirOneSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}	
	
	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List newlist(String  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GForumForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGForumID();			
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseGForum base = (BaseGForum)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
			
			//更新点击次数
			Object[] fields2 = new Object[5];
			
			fields2[0] = base.getAttachcnt() + 1;
			fields2[1] = base.getReplycnt();
			fields2[2] = base.getUpCnt();
			fields2[3] = base.getDownCnt();
			fields2[4] = base.getGForumID();
			
			sql = servSql.getUpdAttachReplycntSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 更新浏览次数失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}			
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加话题到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			//更新发起者积分
			JifenServiceImpl jifenservice = new JifenServiceImpl();
			
			if(theForm.getGroupID() != GlbFinal.PUBLICID){
				//增加话题
				jifenservice.compjf(cn, 14, theForm.getUserID(), msg);
			}else{
				//查找是否存在改社区的资料，如果不存在则第一次增加，1000积分
				GForumForm villageform = new GForumForm();
				
				villageform.setPage(new PageCtr(1, 2, "N", " 1=1 "));
				villageform.setQryVillageID(theForm.getVillageID());
				
				List villagelist = zllist(villageform,msg);
				if(villagelist.size() == 0){
					//增加社区资料积分
					jifenservice.compjf(cn, 7, theForm.getUserID(), msg);
				}
			}			
			if(msg.getErrCode() != 0){
				return;
			}		
			
			//保存附件一
			if(theForm.getFilepath() != null&&!theForm.getFilepath().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setFileID(fileForm.getF_ID());
			}				
	        String intro = StringUtil.htmlTotxt(theForm.getContent());
	        
	        int len = intro.length();
	        if(len > 254){
	        	len = 254;
	        }
	        theForm.setIntro(intro.substring(0,len));
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[26];
			
			theForm.setGForumID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GFORUMID));
			
			fields[0] = theForm.getGForumID();			
			fields[1] = theForm.getGroupID();
			fields[2] = theForm.getVillageID();
			fields[3] = theForm.getTitle();
			fields[4] = theForm.getAuthor();
			fields[5] = theForm.getFrsource();
			fields[6] = theForm.getContent();
			fields[7] = theForm.getIntro();
			fields[8] = theForm.getFileID();
			fields[9] = theForm.getFirsort();
			fields[10] = theForm.getSecsort();
			fields[11] = theForm.getThisort();
			fields[12] = theForm.getStatus();
			fields[13] = theForm.getType();
			fields[14] = GlbFinal.NO;
			fields[15] = GlbFinal.NO;
			fields[16] = theForm.getUpCnt();
			fields[17] = theForm.getDownCnt();
			fields[18] = theForm.getTags();
			fields[19] = 0;
			fields[20] = theForm.getReplycnt();
			fields[21] = DatetimeUtil.getDatetime();
			fields[22] = theForm.getUserID();
			fields[23] = theForm.getIP();
			fields[24] = theForm.getRepuserID();
			fields[25] = DatetimeUtil.getDatetime();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 增加失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			
			//更新群组参与值
			if(theForm.getGroupID() != GlbFinal.PUBLICID){
				GroupsServiceImpl groupservice = new GroupsServiceImpl();
				GroupsForm groupForm = new GroupsForm();
				
				groupForm.Clear();
				groupForm.setGroupID(theForm.getGroupID());
				groupForm.setPartakeVal(1);
				
				groupservice.updNum(cn, groupForm, msg);
				if(msg.getErrCode() != 0){
					
					return;
				}					
			}
			
			//添加用户动态表
			OperLogServiceImpl logservice = new OperLogServiceImpl();
			OperLogForm logForm = new OperLogForm();
			
			logForm.setUserID(theForm.getUserID());
			
			String[] arr = new String[7];
			arr[0] = JstlFun.GetUserNick(theForm.getUserID());			//主操作者		
			arr[1] = "";												//主动者与被动者的关系副词
			arr[2] = "";												//被动接受者ID
			arr[3] = "";												//被动接受者姓名
			if(theForm.getGroupID() != GlbFinal.PUBLICID){
				
				arr[4] = "发表了新话题：";									//操作
				logForm.setType(GlbFinal.OPERTYPE_HUATI);					//群话题
			}else{
				
				arr[4] = "增加了社区资料：";									//操作
				logForm.setType(GlbFinal.OPERTYPE_SQZL);					//社区资料
			}
			arr[5] = theForm.getTitle();								//操作对象名称
			arr[6] = String.valueOf(theForm.getGForumID());				//操作对象KEY，多个字段以'|'分隔

			logForm.setLogarr(arr);
			
			logservice.add(cn, logForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}			
			
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];

			fields[0] = theForm.getGForumID();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGForumService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGForum base = (BaseGForum)list.get(0);	
			BeanUtils.copyProperties(theForm, base);
			
			//删除附件
			if(base.getFileID() != 0){
				FilesServiceImpl fileservice = new FilesServiceImpl();
				FilesForm filesForm = new FilesForm();
				filesForm.Clear();
				filesForm.setF_ID(base.getFileID());
				fileservice.delete(cn, filesForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
			}
			//删除所有回复
			GFollForumServiceImpl follservice = new GFollForumServiceImpl();
			GFollForumForm follForm = new GFollForumForm();
			follForm.setGForumID(theForm.getGForumID());
			
			follservice.delall(cn, follForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			//删除话题
			sql = servSql.getDeleteSql();
			
			boolean res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 删除话题失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(GForumForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			//更新发起者积分
			JifenServiceImpl jifenservice = new JifenServiceImpl();
			
			if(theForm.getGroupID() == GlbFinal.PUBLICID){
				//编辑社区资料
				jifenservice.compjf(cn, 8, theForm.getUserID(), msg);
				if(msg.getErrCode() != 0){
					return;
				}		
			}			
			
			//上传附件
			if(theForm.getFilepath() != null && !theForm.getFilepath().getFileName().equals("")){
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				FilesForm fileForm = new FilesForm();
				
				fileForm.setF_ID(theForm.getFileID());
				fileForm.setFormFile(theForm.getFilepath());
				fileForm.setF_info(theForm.getTitle());
				
				fileservice.update(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setFileID(fileForm.getF_ID());
			}				
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[26];
			
			fields[0] = theForm.getGroupID();
			fields[1] = theForm.getVillageID();
			fields[2] = theForm.getTitle();
			fields[3] = theForm.getAuthor();
			fields[4] = theForm.getFrsource();
			fields[5] = theForm.getContent();
			fields[6] = theForm.getIntro();
			fields[7] = theForm.getFileID();
			fields[8] = theForm.getFirsort();
			fields[9] = theForm.getSecsort();
			fields[10] = theForm.getThisort();
			fields[11] = theForm.getStatus();
			fields[12] = theForm.getType();
			fields[13] = theForm.getUp();
			fields[14] = theForm.getGood();
			fields[15] = theForm.getUpCnt();
			fields[16] = theForm.getDownCnt();
			fields[17] = theForm.getTags();
			fields[18] = theForm.getAttachcnt();
			fields[19] = theForm.getReplycnt();
			fields[20] = DatetimeUtil.getDatetime();
			fields[21] = theForm.getUserID();
			fields[22] = theForm.getIP();
			fields[23] = theForm.getRepuserID();
			fields[24] = DatetimeUtil.getDatetime();
			fields[25] = theForm.getGForumID();
			
			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 话题失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			
			if(theForm.getGroupID() == GlbFinal.PUBLICID){
			
				//添加用户动态表
				OperLogServiceImpl logservice = new OperLogServiceImpl();
				OperLogForm logForm = new OperLogForm();
				
				logForm.setUserID(theForm.getUserID());
				logForm.setType(GlbFinal.OPERTYPE_SQZL);					//社区资料
				
				String[] arr = new String[7];
				arr[0] = JstlFun.GetUserNick(theForm.getUserID());			//主操作者		
				arr[1] = "";												//主动者与被动者的关系副词
				arr[2] = "";												//被动接受者ID
				arr[3] = "";												//被动接受者姓名
				arr[4] = "编辑了社区资料：";									//操作
				arr[5] = theForm.getTitle();								//操作对象名称
				arr[6] = String.valueOf(theForm.getGForumID());				//操作对象KEY，多个字段以'|'分隔

				logForm.setLogarr(arr);
				
				logservice.add(cn, logForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}				
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updUpdown(Connection cn, GForumForm theForm, ReturnMessage msg) 
	{
		
		try
		{

			GForumServiceSqlMysql servSql = new GForumServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[1];

			fields1[0] = theForm.getGForumID();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields1, new DbGForumService());
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGForum base = (BaseGForum)list.get(0);	

			//更新点击次数
			Object[] fields2 = new Object[5];
			
			fields2[0] = base.getAttachcnt();
			fields2[1] = base.getReplycnt();
			fields2[2] = base.getUpCnt() + theForm.getUpCnt();
			fields2[3] = base.getDownCnt()+ theForm.getDownCnt();
			fields2[4] = theForm.getGForumID();
			
			sql = servSql.getUpdAttachReplycntSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 更新浏览次数失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}			

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		
		return ;
	}
}
